//
//  LBLimitedTextView.m
//  LBLimitedTextField
//
//  Created by 刘文扬 on 17/7/3.
//  Copyright © 2017年 lianj. All rights reserved.
//

#import "LBLimitedTextView.h"
#import "LBLimitedTextField.h"

@interface LBLimitedTextView ()<UITextViewDelegate>

/** originalDelegate*/
@property(nonatomic, weak) id<UITextViewDelegate> originalDelegate;

@end

@implementation LBLimitedTextView

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self configue];
}

-(instancetype)init
{
    self = [super init];
    if (self) {
        
        [self configue];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self configue];
        
    }
    return self;
}

-(void)configue
{
    
    self.maxTextLength = NSIntegerMax;
    
    self.delegate = self;
    
    //    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextViewTextDidChangeNotification object:nil];
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    BOOL isDeleting = range.length > 0 && text.length <= 0;
    
    
    
    if (!self.canEmoji) {
        
        /// 过滤emoji
        // 忽略系统默认的emoji键盘
        if ([NSString nowIsEmojiKeyBorad]) {
            return NO;
        }
        
        // 验证string的emoji字符
        if ([text containEmoji]) {
            return NO;
        }
    }
    
    
    
    if (isDeleting || textView.markedTextRange) {
        
        if (self.originalDelegate && [self.originalDelegate respondsToSelector:_cmd]) {
            
            return [self.originalDelegate textView:textView shouldChangeTextInRange:range replacementText:text];
            
        }
        
        return YES;
    }else{
        NSMutableString *matchStr = [NSMutableString stringWithString:textView.text];
        [matchStr insertString:text atIndex:range.location];
        
        
        if (range.location >= self.maxTextLength) {
            //防御措施,不知为何,输入英文的时候,点击联想的时候,即使return no也会显示
            textView.text = [textView.text substringToIndex:self.maxTextLength];
            if (self.lb_degegate && [self.lb_degegate respondsToSelector:@selector(limitedTextViewFieldOverLimitedWithTextView:maxTextLength:)]) {
                [self.lb_degegate limitedTextViewFieldOverLimitedWithTextView:self maxTextLength:self.maxTextLength];
            }
            return NO;
        }
        
        if (self.originalDelegate && [self.originalDelegate respondsToSelector:_cmd]) {
            
            return [self.originalDelegate textView:textView shouldChangeTextInRange:range replacementText:text];
            
        }
        
        
        return YES;
    }
}


-(void)textViewDidChange:(UITextView *)textView
{
    if (!textView.markedTextRange) {
        //输入中文拼音的时候代理无法监听
        if (textView.text.length > self.maxTextLength) {
            textView.text = [textView.text substringToIndex:self.maxTextLength];
            if (self.lb_degegate && [self.lb_degegate respondsToSelector:@selector(limitedTextViewFieldOverLimitedWithTextView:maxTextLength:)]) {
                [self.lb_degegate limitedTextViewFieldOverLimitedWithTextView:self maxTextLength:self.maxTextLength];
            }
        }
        
        
        if (self.lb_degegate && [self.lb_degegate respondsToSelector:@selector(limitedTextView:textCount:)]) {
            [self.lb_degegate limitedTextView:self textCount:self.text.length];
        }
    }
}



-(void)setMarkedText:(NSString *)markedText selectedRange:(NSRange)selectedRange
{
    
    //        //防止在输入法中打出"哈哈"显示出来的"😝",代理
    //        //-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    //        //无法监听
    
    //将含有emoji表情的文字替换为@""
    
    
    
    NSString *noEmojiMarkedText = markedText;
    if (!self.canEmoji) {
        noEmojiMarkedText = [markedText converStrEmoji:markedText];
    }
    [super setMarkedText:noEmojiMarkedText selectedRange:selectedRange];
}

#pragma mark - 重写

-(void)setDelegate:(id<UITextViewDelegate>)delegate
{
    self.originalDelegate = delegate != self ? delegate : nil;
    [super setDelegate:delegate ? self : nil];
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    NSMethodSignature *result = [(id)self.originalDelegate methodSignatureForSelector:aSelector];
    return result;
}


- (void)forwardInvocation:(NSInvocation *)anInvocation {
    if ([(id)self.originalDelegate respondsToSelector:anInvocation.selector]) {
        [anInvocation invokeWithTarget:(id)self.originalDelegate];
    }
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    BOOL a = [super respondsToSelector:aSelector];
    BOOL c = [self.originalDelegate respondsToSelector:aSelector];
    BOOL result = a || c;
    return result;
}

//-(void)dealloc
//{
//    self.originalDelegate = nil;
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
//}




@end
